<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\InfoBox;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class HomeController extends Controller
{
    public function index(Content $content)
    {
        return $content
            ->title('号卡管理中心')
            ->row(function (Row $row) {
                $auth = Auth::guard('admin')->user()->toArray();//获取后端登录用户信息
                if(!$auth['account']){
                    $row->column(2, function (Column $column) {
                        $num = DB::table('users')->count();
                        $column->append(new InfoBox('用户数量', 'users', 'blue', '/zec/Users', $num));
                    });
                    $row->column(2, function (Column $column) {
                        $order_num = DB::table('shop_orders')->whereIn('status', [-1, 1, 2, 3])->count();
                        $column->append(new InfoBox('订单总数', 'users', 'blue', 'zec/Orders', $order_num));
                    });
                    $row->column(2, function (Column $column) {
                        $order_num_status = DB::table('shop_orders')->where('status','2')->count();
                        $column->append(new InfoBox('已激活订单数', 'users', 'blue', 'zec/Orders?status=2', $order_num_status));
                    });
                    $row->column(2, function (Column $column) {
                        $order_num_status = DB::table('users_cash')->where('status','2')->sum('price');
                        $column->append(new InfoBox('已提现金额', 'users', 'blue', 'zec/Cash', $order_num_status));
                    });
                    $row->column(2, function (Column $column) {
                        $order_num_status = DB::table('shop_orders')->whereIn('status',[1, 2])->count();
                        $column->append(new InfoBox('正常订单', 'users', 'blue', 'zec/Orders', $order_num_status));
                    });
                    $row->column(2, function (Column $column) {
                        $order_num_status = DB::table('shop_orders')->where('status','3')->count();
                        $column->append(new InfoBox("异常订单", 'users', 'blue', 'zec/Ordery', $order_num_status));
                    });
                }

            })->row(function (Row $row) {
                // 根据表查询  套餐的所有类型
                $all_prod = DB::table('goods_plan')->get();
                $prod_info = [];
                $auth = Auth::guard('admin')->user()->toArray();//获取后端登录用户信息
                $where = [];
                $orwhere = [];
                if($auth['account']){
                    // 当前登录用户为代理商
                   $user_info = $user_info = DB::table('users')->where(['account'=> $auth['account']])->first();
                   if ( $user_info ) {
                       $where['agent'] = $user_info->id;
                       $orwhere['uid'] = $user_info->id;
                   }
                }
                foreach ( $all_prod as $key=> $value ) {
                    $prod_info[$key]['name'] = $value->planname;
                    $prod_info[$key]['all']  = DB::table('shop_orders')
                        ->where(['prod_id'=> $value->planid])
                        ->where(function($query) use($where, $orwhere){
                            $query->where($where)
                                ->orWhere($orwhere);
                        })
                        ->whereIn('status',[-1,1,2,3])
                        ->count(); // 全部订单
                    $prod_info[$key]['normal']  = DB::table('shop_orders')
                        ->where(['prod_id'=> $value->planid, 'status'=> 1])
                        ->where(function($query) use($where, $orwhere){
                            $query->where($where)
                                ->orWhere($orwhere);
                        })
                        ->count(); // 正常订单
                    $prod_info[$key]['no_normal']  = DB::table('shop_orders')
                        ->where(['prod_id'=> $value->planid, 'status'=> 3])
                        ->where(function($query) use($where, $orwhere){
                            $query->where($where)
                                ->orWhere($orwhere);
                        })
                        ->count(); // 异常订单
                    $prod_info[$key]['active']  = DB::table('shop_orders')
                        ->where(['prod_id'=> $value->planid, 'status'=> 2])
                        ->where(function($query) use($where, $orwhere){
                            $query->where($where)
                                ->orWhere($orwhere);
                        })
                        ->count(); // 激活订单
                    $prod_info[$key]['cancle']  = DB::table('shop_orders')
                        ->where(['prod_id'=> $value->planid, 'status'=> -1])
                        ->where(function($query) use($where, $orwhere){
                            $query->where($where)
                                ->orWhere($orwhere);
                        })
                        ->count(); // 取消订单
                }
                $html = '<style>
			.define-table{
				border-collapse:collapse;
				border-spacing:0;
				border-left:1px solid #888;
				border-top:1px solid #888;
			}
			.define-table th,.define-table td{
				border-right:1px solid #888;
				border-bottom:1px solid #888;
				padding:5px 15px;
			}
			.define-table th{
				font-weight:bold;background:#ccc;
			}	
		</style>
        <div style="margin:0 auto;text-align: center;"><table class="define-table" style="width: 100%;">
                    <tr>
                        <th></th>
                        <th>总订单数</th>
                        <th>正常订单数</th>
                        <th>异常订单数</th>
                        <th>激活订单数</th>
                        <th>取消订单数</th>
                    </tr>';
                $table_tr = '';
                foreach ($prod_info as $key=> $value) {
                    if($value['all'] != 0){
                        $table_tr .= '<tr>
                            <td>'.$value['name'].'</td>
                            <td>'.$value['all'].'</td>
                            <td>'.$value['normal'].'</td>
                            <td>'.$value['no_normal'].'</td>
                            <td>'.$value['active'].'</td>
                            <td>'.$value['cancle'].'</td>
                        </tr>';
                    }

                }
                if($table_tr == ''){
                    $table_tr = '<tr><td colspan="5">暂无订单数据</td></tr>';
                }
                $html .= $table_tr;
                $html .= '</table></div>';
                $row->column(12, $html);
//                $row->column(6, function (Column $column) {
//                    $column->append(Dashboard::environment());
//                });

//                $row->column(6, function (Column $column) {
//                    $column->append(Dashboard::dependencies());
//                });
            });
    }
}
